-- 智慧医疗管理系统 XMake 配置文件
set_project("SmartMedicalSystem")
set_version("1.0.0")
set_xmakever("2.5.1")

-- 设置 C++ 标准
set_languages("c++11")

-- 添加编译模式
add_rules("mode.debug", "mode.release")

-- 设置输出目录
set_targetdir("bin")

-- 添加编译选项（Qt 需要 -fPIC）
add_cxflags("-fPIC")

-- Qt 路径配置
local qt_inc = "/usr/include/x86_64-linux-gnu/qt5"
local qt_lib = "/usr/lib/x86_64-linux-gnu"

-- ========== Common 静态库 ==========
target("Common")
    set_kind("static")
    
    -- 源文件
    add_files("Common/protocol.cpp", "Common/databasemanager.cpp", "Common/models.cpp")
    add_headerfiles("Common/*.h")
    
    -- 头文件搜索路径
    add_includedirs("Common", {public = true})
    add_includedirs(qt_inc)
    add_includedirs(qt_inc .. "/QtCore")
    add_includedirs(qt_inc .. "/QtNetwork")
    add_includedirs(qt_inc .. "/QtSql")
    
    -- 定义宏
    add_defines("QT_CORE_LIB", "QT_NETWORK_LIB", "QT_SQL_LIB")
    
    -- 添加 Qt 库依赖（虽然是静态库，但需要传递给依赖它的目标）
    add_syslinks("Qt5Sql", "Qt5Network", "Qt5Core")
    
target_end()

-- ========== Server 可执行文件 ==========
target("MedicalServer")
    set_kind("binary")
    
    -- 依赖 Common 库
    add_deps("Common")
    
    -- 源文件（包括 moc 生成的文件）
    add_files("Server/main.cpp", "Server/medicalserver.cpp", "Server/clienthandler.cpp")
    add_files("Server/moc_*.cpp")
    add_headerfiles("Server/*.h")
    
    -- 头文件搜索路径
    add_includedirs("Server", "Common")
    add_includedirs(qt_inc)
    add_includedirs(qt_inc .. "/QtCore")
    add_includedirs(qt_inc .. "/QtNetwork")
    add_includedirs(qt_inc .. "/QtSql")
    
    -- 定义宏
    add_defines("QT_CORE_LIB", "QT_NETWORK_LIB", "QT_SQL_LIB")
    
    -- 在编译前生成 MOC 文件
    on_load(function (target)
        local moc = "/usr/lib/qt5/bin/moc"
        local moc_args = "-DQT_CORE_LIB -DQT_NETWORK_LIB -DQT_SQL_LIB " ..
                        "-I" .. qt_inc .. " -I" .. qt_inc .. "/QtCore " ..
                        "-I" .. qt_inc .. "/QtNetwork -I" .. qt_inc .. "/QtSql " ..
                        "-IServer -ICommon"
        
        -- 生成 moc 文件
        os.exec(moc .. " " .. moc_args .. " Server/medicalserver.h -o Server/moc_medicalserver.cpp")
        os.exec(moc .. " " .. moc_args .. " Server/clienthandler.h -o Server/moc_clienthandler.cpp")
    end)
    
    -- 链接库（顺序很重要：先链接依赖的库，再链接被依赖的库）
    add_linkdirs(qt_lib)
    add_links("Qt5Sql", "Qt5Network", "Qt5Core", "pthread")
    
target_end()

-- ========== DoctorClient 可执行文件（已废弃，使用 MedicalClient）==========
--[[
target("DoctorClient")
    set_kind("binary")
    
    -- 依赖 Common 库
    add_deps("Common")
    
    -- 源文件（包括 moc 生成的文件）
    add_files("DoctorClient/*.cpp")
    add_files("DoctorClient/moc_*.cpp")
    add_headerfiles("DoctorClient/*.h")
    
    -- 头文件搜索路径
    add_includedirs("DoctorClient", "Common")
    add_includedirs(qt_inc)
    add_includedirs(qt_inc .. "/QtCore")
    add_includedirs(qt_inc .. "/QtGui")
    add_includedirs(qt_inc .. "/QtWidgets")
    add_includedirs(qt_inc .. "/QtNetwork")
    add_includedirs(qt_inc .. "/QtSql")
    
    -- 定义宏
    add_defines("QT_CORE_LIB", "QT_GUI_LIB", "QT_WIDGETS_LIB", "QT_NETWORK_LIB", "QT_SQL_LIB")
    
    -- 在加载时生成 UI 和 MOC 文件
    on_load(function (target)
        local uic = "/usr/lib/qt5/bin/uic"
        local moc = "/usr/lib/qt5/bin/moc"
        local moc_args = "-DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_SQL_LIB -DQT_CORE_LIB " ..
                        "-I" .. qt_inc .. " -I" .. qt_inc .. "/QtWidgets -I" .. qt_inc .. "/QtGui " ..
                        "-I" .. qt_inc .. "/QtNetwork -I" .. qt_inc .. "/QtSql -I" .. qt_inc .. "/QtCore " ..
                        "-IDoctorClient -ICommon"
        
        -- 处理 UI 文件
        local ui_files = os.files("DoctorClient/*.ui")
        for _, ui_file in ipairs(ui_files) do
            local ui_name = path.basename(ui_file)
            local ui_header = "DoctorClient/ui_" .. ui_name .. ".h"
            os.exec(uic .. " " .. ui_file .. " -o " .. ui_header)
        end
        
        -- 处理 MOC 文件
        local headers = {"logindialog.h", "mainwindow.h", "networkclient.h", 
                        "appointmentwidget.h", "medicalrecordwidget.h", 
                        "prescriptionwidget.h", "examrequestwidget.h"}
        
        for _, header in ipairs(headers) do
            local moc_file = "DoctorClient/moc_" .. path.basename(header) .. ".cpp"
            os.exec(moc .. " " .. moc_args .. " DoctorClient/" .. header .. " -o " .. moc_file)
        end
    end)
    
    -- 链接库（顺序很重要：先链接依赖的库，再链接被依赖的库）
    add_linkdirs(qt_lib)
    add_links("Qt5Sql", "Qt5Network", "Qt5Widgets", "Qt5Gui", "Qt5Core", "GL", "pthread")
    
target_end()
]]--

-- ========== PatientClient 可执行文件（已废弃，使用 MedicalClient）==========
--[[
target("PatientClient")
    set_kind("binary")
    
    -- 依赖 Common 库
    add_deps("Common")
    
    -- 源文件（包括 moc 生成的文件）
    add_files("PatientClient/*.cpp")
    add_files("PatientClient/moc_*.cpp")
    add_headerfiles("PatientClient/*.h")
    
    -- 头文件搜索路径
    add_includedirs("PatientClient", "Common")
    add_includedirs(qt_inc)
    add_includedirs(qt_inc .. "/QtCore")
    add_includedirs(qt_inc .. "/QtGui")
    add_includedirs(qt_inc .. "/QtWidgets")
    add_includedirs(qt_inc .. "/QtNetwork")
    add_includedirs(qt_inc .. "/QtSql")
    
    -- 定义宏
    add_defines("QT_CORE_LIB", "QT_GUI_LIB", "QT_WIDGETS_LIB", "QT_NETWORK_LIB", "QT_SQL_LIB")
    
    -- 在加载时生成 UI 和 MOC 文件
    on_load(function (target)
        local uic = "/usr/lib/qt5/bin/uic"
        local moc = "/usr/lib/qt5/bin/moc"
        local moc_args = "-DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_SQL_LIB -DQT_CORE_LIB " ..
                        "-I" .. qt_inc .. " -I" .. qt_inc .. "/QtWidgets -I" .. qt_inc .. "/QtGui " ..
                        "-I" .. qt_inc .. "/QtNetwork -I" .. qt_inc .. "/QtSql -I" .. qt_inc .. "/QtCore " ..
                        "-IPatientClient -ICommon"
        
        -- 处理 UI 文件
        local ui_files = os.files("PatientClient/*.ui")
        for _, ui_file in ipairs(ui_files) do
            local ui_name = path.basename(ui_file)
            local ui_header = "PatientClient/ui_" .. ui_name .. ".h"
            os.exec(uic .. " " .. ui_file .. " -o " .. ui_header)
        end
        
        -- 处理 MOC 文件
        local headers = {"logindialog.h", "registerdialog.h", "mainwindow.h", "networkclient.h",
                        "appointmentwidget.h", "medicalrecordviewwidget.h", 
                        "prescriptionviewwidget.h", "paymentwidget.h"}
        
        for _, header in ipairs(headers) do
            local moc_file = "PatientClient/moc_" .. path.basename(header) .. ".cpp"
            os.exec(moc .. " " .. moc_args .. " PatientClient/" .. header .. " -o " .. moc_file)
        end
    end)
    
    -- 链接库（顺序很重要：先链接依赖的库，再链接被依赖的库）
    add_linkdirs(qt_lib)
    add_links("Qt5Sql", "Qt5Network", "Qt5Widgets", "Qt5Gui", "Qt5Core", "GL", "pthread")
    
target_end()
]]--

-- ========== MedicalClient 统一客户端 ==========
target("MedicalClient")
    set_kind("binary")
    
    -- 依赖 Common 库
    add_deps("Common")
    
    -- 源文件（包括所有子目录）
    add_files("MedicalClient/*.cpp")
    add_files("MedicalClient/network/*.cpp")
    add_files("MedicalClient/dialogs/*.cpp")
    add_files("MedicalClient/widgets/doctor/*.cpp")
    add_files("MedicalClient/widgets/patient/*.cpp")
    add_files("MedicalClient/widgets/common/*.cpp")
    -- 新界面源文件
    
    -- 头文件
    add_headerfiles("MedicalClient/*.h")
    add_headerfiles("MedicalClient/network/*.h")
    add_headerfiles("MedicalClient/dialogs/*.h")
    add_headerfiles("MedicalClient/widgets/doctor/*.h")
    add_headerfiles("MedicalClient/widgets/patient/*.h")
    add_headerfiles("MedicalClient/widgets/common/*.h")
    
    -- 头文件搜索路径
    add_includedirs("MedicalClient", "MedicalClient/network", "MedicalClient/dialogs", 
                    "MedicalClient/widgets/doctor", "MedicalClient/widgets/patient", 
                    "MedicalClient/widgets/common", "Common")
    add_includedirs(qt_inc)
    add_includedirs(qt_inc .. "/QtCore")
    add_includedirs(qt_inc .. "/QtGui")
    add_includedirs(qt_inc .. "/QtWidgets")
    add_includedirs(qt_inc .. "/QtNetwork")
    add_includedirs(qt_inc .. "/QtSql")
    
    -- 定义宏
    add_defines("QT_CORE_LIB", "QT_GUI_LIB", "QT_WIDGETS_LIB", "QT_NETWORK_LIB", "QT_SQL_LIB")
    
    -- 在加载时生成 UI 和 MOC 文件
    on_load(function (target)
        local uic = "/usr/lib/qt5/bin/uic"
        local moc = "/usr/lib/qt5/bin/moc"
        local moc_args = "-DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_SQL_LIB -DQT_CORE_LIB " ..
                        "-I" .. qt_inc .. " -I" .. qt_inc .. "/QtWidgets -I" .. qt_inc .. "/QtGui " ..
                        "-I" .. qt_inc .. "/QtNetwork -I" .. qt_inc .. "/QtSql -I" .. qt_inc .. "/QtCore " ..
                        "-IMedicalClient -IMedicalClient/network -IMedicalClient/dialogs " ..
                        "-IMedicalClient/widgets/doctor -IMedicalClient/widgets/patient " ..
                        "-IMedicalClient/widgets/common -ICommon"
        
        -- 处理所有子目录中的 UI 文件
        local ui_dirs = {"MedicalClient/dialogs", "MedicalClient/widgets/doctor", 
                        "MedicalClient/widgets/patient", "MedicalClient/widgets/common"}
        for _, dir in ipairs(ui_dirs) do
            local ui_files = os.files(dir .. "/*.ui")
            for _, ui_file in ipairs(ui_files) do
                local ui_name = path.basename(ui_file)
                local ui_header = dir .. "/ui_" .. ui_name .. ".h"
                os.exec(uic .. " " .. ui_file .. " -o " .. ui_header)
            end
        end
        
        -- 处理所有需要 MOC 的头文件
        local moc_headers = {
            {"MedicalClient/network", {"networkclient.h"}},
            {"MedicalClient/dialogs", {"logindialog.h", "registerdialog.h"}},
            {"MedicalClient/widgets/doctor", {"doctorwidget.h", "appointmentwidget.h", "attendancewidget.h",
                                              "doctorprofilewidget.h", "medicalrecordwidget.h", 
                                              "prescriptionwidget.h", "medicineselectdialog.h", "doctor_chatlistwidget.h"}},
            {"MedicalClient/widgets/patient", {"patientwidget.h", "patient_appointmentwidget.h", 
                                               "patientprofilewidget.h", "patient_chatlistwidget.h",
                                               "medicinesearchwidget.h"}},
            {"MedicalClient/widgets/common", {"chatwindow.h", "patientdetailwidget.h", "doctordetailwidget.h",
                                              "medicalrecorddetailwidget.h", "medicalrecordviewwidget.h",
                                              "prescriptiondetailwidget.h", "prescriptionviewwidget.h"}}
        }
        
        for _, entry in ipairs(moc_headers) do
            local dir = entry[1]
            local headers = entry[2]
            for _, header in ipairs(headers) do
                local moc_file = dir .. "/moc_" .. header .. ".cpp"
                os.exec(moc .. " " .. moc_args .. " " .. dir .. "/" .. header .. " -o " .. moc_file)
            end
        end
    end)
    
    -- 链接库（顺序很重要：先链接依赖的库，再链接被依赖的库）
    add_linkdirs(qt_lib)
    add_links("Qt5Sql", "Qt5Network", "Qt5Widgets", "Qt5Gui", "Qt5Core", "GL", "pthread")
    
target_end()
